/**
 * Copyright 2019 [shujiang.yang], Inc. All rights reserved.
 * Email: 314938230@qq.com
**/
define("pages/ryzpgl/ryzpgl_jbxx_form", [
        "../../plugins/easyui/jquery-easyui-1.5.1/jquery.easyui.min",
        "../../plugins/easyui/jquery-easyui-1.5.1/easyloader",
        '../../pages/ryzpgl/ryzpgl_form.html.js',
        "../../common/componts",
        "../../common/tools"
    ],
    function(require){
        var layerIndex;
        var _callback;
        var fileList={};

        var formTemplate = require("./ryzpgl_form.html");

        /**
         * 获取表单内容
         * @param initData
         * @returns {*}
         */
        var getFormHtml = function(initData){
            var htmlCompile = template.compile(formTemplate);
            var username = CURRENT_USER.username;//获取当前办理人
            var html = htmlCompile({
                tbr:username,
                tbrq:currentDate(),
                data:initData
            });
            return html;
        };
        /**
         * 动态渲染
         */
        var parseEasyUIElement = function(){
            $.parser.parse($(".awd-form-panel"));
        };

        var _show = function(initData,callback){
            _callback = callback;
            var componts = require("../../common/componts");

            layerIndex = layer.open({
                type: 1,
                title: "数据录入",
                skin: 'layui-layer-lan', //加上边框
                closeBtn: 1, //不显示关闭按钮
                shadeClose: true, //开启遮罩关闭
                content: getFormHtml(initData),
                area:['100%','100%'],
                success: function(layero, index){
                    console.log(layero, index);
                }
            });
            layer.full(layerIndex);
            setTimeout(function(){
                //初始化下拉框
                componts.initSelectPage(undefined);
                componts.initSelect(undefined,undefined,undefined);
                parseEasyUIElement(); //动态渲染

                $(".person-photo").change(function(){
                    var $me = $(this);
                    var id = $me.attr("id");
                    console.log('xxxxxxxxxxxxxx');
                    var file = $("#"+id)[0].files[0];
                    console.log(file);
                    if (file) {
                        var img = document.getElementById(id+"Image");
                        img.onload = function(){
                            img.width  =  250;
                            img.height =  125;
                        };
                        var reader = new FileReader();
                        reader.onload = function(evt){img.src = evt.target.result;}
                        reader.readAsDataURL(file);
                    }
                })

            },100);

            $('#centerPhoto').change(function(){
                var  str=$(this).val();
                fileList.centerPhoto=str;
                console.log("1"+str);
            })
            $('#leftPhoto').change(function(){
                var  str=$(this).val();
                fileList.leftPhoto=str;
                console.log("2"+str);
            })
            $('#rightPhoto').change(function(){
                var  str=$(this).val();
                fileList.rightPhoto=str;
                console.log("3"+str);
            })
        };

        var save = function(){
            var url = "/ryzpgl/save";
            var $form = $(".awd-form");
            if (!$form.form('validate')) {
                $.messager.alert("提示", '必填项不能为空！');
                return;
            }

            console.log("-------------=="+JSON.stringify(fileList));
            var filelsitJsonStr=JSON.stringify(fileList);

            var data = $form.serialize();
            console.log('form data -> ',data);
            var loadingIndex = layer.load();
            $.ajax({
                type : "POST",  //提交方式
                url : url,
                data : {data:data,filelsitJsonStr:filelsitJsonStr},//数据，这里使用的是Json格式进行传输
                success : function(result) {
                    layer.closeAll();
                    if (result.success) {
                        _callback && _callback();
                        $.messager.alert("确定","保存成功","info");
                    }else{
                        $.messager.alert("确定","保存失败","warn");
                    }
                },
                complete:function(){
                    layer.close(loadingIndex);
                }
            });
        };

        var bindAction = function(){
            //保存按钮事件
            $(document).on('click','.save-button',function () {
                save();
            });
            //点击关闭按钮
            $(document).on('click','.close-button',function(){
                layer.close(layerIndex);
                layerIndex = null;
            });
            //修正layer 最大化，解决办法..
            $(window).resize(function(){
                layerIndex && layer.full(layerIndex);
                parseEasyUIElement();//TODO::后续优化
            })
        };

        (function(){
            var componts = require("../../common/componts");
            componts.initSelectPage(undefined);
            componts.initSelect(undefined,undefined,undefined);
            bindAction();
        })()

        return{
            show:_show
        };
    });